require 'fox16'
require 'active_record'
ActiveRecord::Base.establish_connection(
      :adapter => "sqlite3" ,
      :database => "../db/development.sqlite3"
)
include Fox

class Gsclass < ActiveRecord::Base
end
class Character < ActiveRecord::Base
end
class Classkey < ActiveRecord::Base
end

class CharacterKeyGen < FXMainWindow
  def initialize(app)
    super(app, "Character Key Generator", :width => 400, :height => 400)
    charid=0,
    classname=""
    classID=0
    classkey=0
    @splitter = FXSplitter.new(self, :opts => LAYOUT_FILL)
    @splitter.splitterStyle|=SPLITTER_VERTICAL
    hmainframe=FXHorizontalFrame.new(@splitter,:opts=>LAYOUT_FILL)
    text = FXText.new(@splitter, :opts => TEXT_WORDWRAP|TEXT_AUTOSCROLL|TEXT_READONLY|LAYOUT_FILL|FRAME_SUNKEN)
    vchild=FXVerticalFrame.new(hmainframe,:opts=>LAYOUT_FILL)
    charCombo=FXListBox.new(vchild,:opts => LISTBOX_NORMAL|FRAME_SUNKEN|LAYOUT_FILL_X)
    charCombo.numVisible = 7
    @chars=Character.find(:all)
    for x in @chars
      charCombo.appendItem(x.name.to_s)
    end
    classCombo=FXListBox.new(vchild,:opts => LISTBOX_NORMAL|FRAME_SUNKEN|LAYOUT_FILL_X)
    classCombo.numVisible = 7
    @classes=Gsclass.find(:all)
     for x in @classes
      classCombo.appendItem(x.name.to_s)
    end
    charCombo.connect(SEL_COMMAND) do |sender,sel,data|
      puts "Charater:",data,"\n"
      charid=data
      
    end
    classCombo.connect(SEL_COMMAND) do |sender,sel,data|
      puts "Class",classCombo.getItemText(data),"\n"
      classname=classCombo.getItemText(data)
      classID=Gsclass.find_by_name(classname).id
    end
    earthList=FXList.new(hmainframe,:opts => FRAME_SUNKEN|FRAME_THICK)
    fireList=FXList.new(hmainframe,:opts => LISTBOX_NORMAL|FRAME_SUNKEN)
    windList=FXList.new(hmainframe,:opts => LISTBOX_NORMAL|FRAME_SUNKEN)
    waterList=FXList.new(hmainframe,:opts => LISTBOX_NORMAL|FRAME_SUNKEN)
    0.upto(7) { |x| earthList.appendItem(x.to_s) }
    0.upto(7) { |x| fireList.appendItem(x.to_s) }
    0.upto(7) { |x| windList.appendItem(x.to_s) }
    0.upto(7) { |x| waterList.appendItem(x.to_s) }
    earthList.numVisible = 8
    fireList.numVisible = 8
    windList.numVisible = 8
    waterList.numVisible = 8
    addKeys=FXButton.new(hmainframe,"Add Keys",:opts=> BUTTON_NORMAL|LAYOUT_SIDE_BOTTOM)
    addKeys.connect(SEL_COMMAND) do
      @selEarth=Array.new
      @selFire=Array.new
      @selWind=Array.new
      @selWater=Array.new
      # this is where the Keys are gereated
      #key = (charid*8**4)+(earth*8**3)+(fire*8**2)+(wind*8)+(earth)
      #the key is generated for each combination of selected items with a max of E+F+Wi+Wa<=7
       earthList.each { |item| @selEarth << item.to_s if item.selected? }
       fireList.each { |item| @selFire << item.to_s if item.selected? }
       windList.each { |item| @selWind << item.to_s if item.selected? }
       waterList.each { |item| @selWater << item.to_s if item.selected? }
       #for i in 0...@songs.length
       
       
       for e in @selEarth
         for f in @selFire
           for wi in @selWind
             for wa in @selWater
               if (e.to_i+f.to_i+wi.to_i+wa.to_i<=7)
                 classkey=(charid*8**4)+(e.to_i*8**3)+(f.to_i*8**2)+(wi.to_i*8)+(wa.to_i)
                 @ck=Classkey.new(:gsclass_id=>classID,:key=>classkey)
                 text.appendText("Key:"+classkey.to_s+"\tCharid:"+charid.to_s+"\tClassID:"+classID.to_s+"\tClassname:"+classname.to_s+"\n",true)
                 @ck.save
               end
             end
           end
         end
       end
      
    end
    self.connect(SEL_CLOSE) do

      app.reg.writeIntEntry("Settings", "splitSize", @splitter.getSplit(0))
      app.exit(0)

    end
  end
  

  def create
    super
    @splitter.setSplit(0, app.reg.readIntEntry("Settings", "splitSize"))
    show(PLACEMENT_SCREEN)

  end

end

if __FILE__ == $0
  FXApp.new do |app|
    CharacterKeyGen.new(app)
    app.create
    app.run
  end
end
